<?php
session_start();

if(!isset($_SESSION['last_key'])) {
  echo '{"error_code":0, "error":"Not logged in"}';
  return;
}

include_once('config.php');
include_once('weibooauth.php');

$c = new WeiboClient(WB_AKEY, WB_SKEY, $_SESSION['last_key']['oauth_token'], $_SESSION['last_key']['oauth_token_secret']);

const TYPE_PUBLIC    = 'public';
const TYPE_FRIENDS   = 'friends';
const TYPE_USER      = 'user';
const TYPE_MENTIONS  = 'mentions';
//const REPOST    = 'repost'; // fwd list of specified wb

$type    = isset($_GET['type']) ? $_GET['type'] : TYPE_PUBLIC;
$user_id = isset($_GET['user_id']) && $_GET['user_id'] != 0 ? $_GET['user_id'] : $_SESSION['id']; // id or nick name
$page    = isset($_GET['page']) ? $_GET['page'] : WB_START_PAGE;
$count   = isset($_GET['count']) ? $_GET['count'] : WB_COUNT_EACH_PAGE;

if ($type == TYPE_PUBLIC) {
  $msg = $c->public_timeline($count);
} else if ($type == TYPE_USER) {
  $msg = $c->user_timeline($page, $count, $user_id);
} else if ($type == TYPE_FRIENDS) {
  $msg = $c->home_timeline_page($page, $count);
} else if ($type == TYPE_MENTIONS) {
  $msg = $c->mentions($page, $count);
  if ($page == WB_START_PAGE) {
    $c->clear_unread(2);//mentions
  }
} //else if ($type == TimeLineType::REPOST) {
  //$msg = $c->home_timeline_page($page, $count);
//}

$sid_total = '';
$msg_count = count($msg);
for ($i = 0; $i < $msg_count; ++$i) {
  $sid_total .= $msg[$i]['id'];
  if ($i != $msg_count - 1) {
    $sid_total .= ",";
  }

  $slim[$i]['created_at'] = $msg[$i]['created_at'];
  $slim[$i]['id'] = $msg[$i]['id'];
  $slim[$i]['text'] = $msg[$i]['text'];
  $slim[$i]['comments'] = 0;
  $slim[$i]['rt'] = 0;
  if (isset($msg[$i]['thumbnail_pic'])) {
    $slim[$i]['thumbnail_pic'] = $msg[$i]['thumbnail_pic'];
    $slim[$i]['bmiddle_pic'] = $msg[$i]['bmiddle_pic'];
    $slim[$i]['original_pic'] = $msg[$i]['original_pic'];
  }
  $slim[$i]['user']['id'] = $msg[$i]['user']['id'];
  $slim[$i]['user']['name'] = $msg[$i]['user']['name'];
  $slim[$i]['user']['profile_image_url'] = $msg[$i]['user']['profile_image_url'];
  $slim[$i]['user']['verified'] = $msg[$i]['user']['verified'];
  if (isset($msg[$i]['retweeted_status'])) {
    $slim[$i]['retweeted_status']['created_at'] = $msg[$i]['retweeted_status']['created_at'];
    $slim[$i]['retweeted_status']['id'] = $msg[$i]['retweeted_status']['id'];
    $slim[$i]['retweeted_status']['text'] = $msg[$i]['retweeted_status']['text'];
    if (isset($msg[$i]['retweeted_status']['thumbnail_pic'])) {
      $slim[$i]['retweeted_status']['thumbnail_pic'] = $msg[$i]['retweeted_status']['thumbnail_pic'];
      $slim[$i]['retweeted_status']['bmiddle_pic'] = $msg[$i]['retweeted_status']['bmiddle_pic'];
      $slim[$i]['retweeted_status']['original_pic'] = $msg[$i]['retweeted_status']['original_pic'];
    }
    $slim[$i]['retweeted_status']['user']['id'] = $msg[$i]['retweeted_status']['user']['id'];
    $slim[$i]['retweeted_status']['user']['name'] = $msg[$i]['retweeted_status']['user']['name'];
    $slim[$i]['retweeted_status']['user']['profile_image_url'] = $msg[$i]['retweeted_status']['user']['profile_image_url'];
    $slim[$i]['retweeted_status']['user']['verified'] = $msg[$i]['retweeted_status']['user']['verified'];
  }
}

if ($msg_count > 0) {
  $msg_cmt_num = $c->get_count_info_by_ids($sid_total);
  if (!isset($msg_cmt_num['error_code']) && !isset($msg_cmt_num['error'])){
    foreach($msg_cmt_num as $cmt_note){
      for ($i = 0; $i < $msg_count; ++$i) {
        if ($slim[$i]['id'] == $cmt_note['id']) {
          $slim[$i]['comments'] = $cmt_note['comments'];
          $slim[$i]['rt'] = $cmt_note['rt'];
          break;
        }
      }
    }
  }
}

echo json_encode($slim);
?>